home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
edit
/
tde40.zip
/
config.c
< prev
next >
Wrap
C/C++ Source or Header
|
1994-06-05
|
41KB
|
1,153 lines
/*
* This file contains the utilities to read in a ".tdecfg" file.
*
* Most of this stuff is duplicated from the cfgfile.c functions. In
* Linux, this utility searches the CWD first then it searches the
* HOME directory for the ".tdecfg" file.
*
* Many thanks to <intruder@link.hacktic.nl> for the idea and sample code
* for this function.
*
* New editor name: TDE, the Thomson-Davis Editor.
* Author: Frank Davis
* Date: June 5, 1991, version 1.0
* Date: July 29, 1991, version 1.1
* Date: October 5, 1991, version 1.2
* Date: January 20, 1992, version 1.3
* Date: February 17, 1992, version 1.4
* Date: April 1, 1992, version 1.5
* Date: June 5, 1992, version 2.0
* Date: October 31, 1992, version 2.1
* Date: April 1, 1993, version 2.2
* Date: June 5, 1993, version 3.0
* Date: August 29, 1993, version 3.1
* Date: November 13, 1993, version 3.2
* Date: June 5, 1994, version 4.0
*
* This code is released into the public domain, Frank Davis.
* You may distribute it freely.
*/
#include "tdestr.h" /* tde types */
#include "common.h"
#include "define.h"
#include "tdefunc.h"
#if defined( __UNIX__ )
#include "cfgfile.h"
#endif
#if !defined( __UNIX__ )
#include <dos.h> /* for renaming files */
#include <bios.h> /* for direct BIOS keyboard input */
#include <io.h> /* for file attribute code */
#if defined( __MSC__ )
#include <errno.h>
#include <sys\types.h> /* S_IWRITE etc */
#endif
#include <sys\stat.h> /* S_IWRITE etc */
#endif
#include <fcntl.h> /* open flags */
#if defined( __UNIX__ )
char *line_in; /* line buffer */
char *line_out; /* line buffer */
int stroke_count; /* global variable for macro strokes */
unsigned int line_no; /* global variable for line count */
int need_a_redraw; /* if we redefined colors, then redraw screen */
int need_mode_line; /* if we redefined modes, then redraw line */
int need_rulers; /* if we redefined rulers, then redraw ruler */
/*
* UNIX stuff: let's make us an array of colors used in curses.
*/
int curse_col[8] = { COLOR_BLACK, COLOR_RED, COLOR_GREEN, COLOR_YELLOW,
COLOR_BLUE, COLOR_MAGENTA, COLOR_CYAN, COLOR_WHITE };
#endif
/*
* Name: tdecfgfile
* Date: June 5, 1994
* Notes: read in a configuration file at any time.
* read in a configuration file when we first fire up TDE in
* a linux (unix) environment.
*/
int tdecfgfile( TDE_WIN *window )
{
FILE *config;
int rc;
int prompt_line;
int *clr;
#if defined( __UNIX__ )
char fname[PATH_MAX]; /* new name for file */
char *home;
#else
char fname[MAX_COLS]; /* new name for file */
#endif
rc = OK;
#if defined( __UNIX__ )
if (window != NULL)
prompt_line = window->bottom_line;
else
prompt_line = g_display.nlines;
/*
* first, make sure we can alloc space for line buffers.
* line buffers are needed for reading the config file.
*/
line_in = my_malloc( MAX_LINE_LENGTH - 1, &rc );
if (rc == OK)
line_out = my_malloc( g_display.ncols + 2, &rc );
else
line_out = NULL;
if (rc == OK) {
/*
* prompt for the configuration file name.
*/
#if defined( __UNIX__ )
/*
* in Linux, we search for a ".tdecfg" file in
* 1) current working directory == "."
* 2) the user's home directory.
*
* CONFIGFILE is defined in tdestr.h
*/
strcpy( fname, "." );
strcat( fname, "/" );
strcat( fname, CONFIGFILE );
if (access( fname, F_OK ) != 0) {
/*
* could not find config file in cwd. try user's home directory.
*/
home = (char *)getenv( "HOME" );
if (home == NULL)
rc = ERROR;
else {
strcpy( fname, home );
strcat( fname, "/" );
strcat( fname, CONFIGFILE );
}
}
#else
*fname = '\0';
rc = get_name( config1, prompt_line, fname, g_display.message_color );
#endif
if (rc == OK) {
if ((config = fopen( fname, "r" )) == NULL) {
rc = ERROR;
if (window != NULL) {
combine_strings( line_out, main7a, fname, main7b );
error( WARNING, prompt_line, line_out );
}
}
/*
* if everything is everthing so far, get the current editor settings.
*/
if (rc == OK) {
need_a_redraw = FALSE;
need_mode_line = FALSE;
need_rulers = FALSE;
stroke_count = get_stroke_count( );
line_no = 1;
while (!feof( config )) {
if (fgets( line_in, 1500, config ) == NULL)
break;
/*
* for convenience, let's remove the <cr><lf> pair from
* from MSDOS-type text files.
*/
#if defined( __UNIX__ )
remove_cr( line_in );
#endif
parse_line( line_in, prompt_line );
++line_no;
}
fclose( config );
if (need_a_redraw && g_display.adapter != MDA) {
clr = &colour.clr[1][0];
g_display.head_color = *clr++;
g_display.text_color = *clr++;
g_display.dirty_color = *clr++;
g_display.mode_color = *clr++;
g_display.block_color = *clr++;
g_display.message_color = *clr++;
g_display.help_color = *clr++;
g_display.diag_color = *clr++;
g_display.eof_color = *clr++;
g_display.curl_color = *clr++;
g_display.ruler_color = *clr++;
g_display.ruler_pointer = *clr++;
g_display.hilited_file = *clr++;
g_display.overscan = *clr;
if (window != NULL)
redraw_screen( window );
}
if (need_mode_line && window != NULL)
show_modes( );
if (need_rulers && window != NULL)
show_all_rulers( );
}
}
} else {
/*
* not enough memory
*/
error( WARNING, prompt_line, main4 );
rc = ERROR;
}
if (line_in != NULL)
my_free( line_in );
if (line_out != NULL)
my_free( line_out );
#endif
return( rc );
}
#if defined( __UNIX__ )
/*
* Name: remove_cr
* Purpose: get rid of <cr>
* Date: June 5, 1994
* Passed: line: line of text
* Notes: UNIX don't like <cr><lf>. if we read MSDOS config files,
* we need to get rid of the <cr><lf> thing. change <cr> to <lf>.
*/
void remove_cr( char *line )
{
if (line != NULL) {
while (*line) {
if (*line == '\r')
*line = '\n';
++line;
}
}
}
/*
* Name: parse_line
* Purpose: real work horse of the configuration utility, figure out what
* we need to do with each line of the config file.
* Date: June 5, 1994
* Passed: line: line that contains the text to parse
*/
void parse_line( char *line, int prompt_line )
{
char key[1042]; /* buffer to hold any token that we parse */
char *residue; /* pointer to next item in line, if it exists */
int key_no; /* index into key array */
int parent_key; /* 1st of two-combination keys */
int color; /* color field */
int mode_index; /* index in mode array */
int func_no; /* function number we want to assign to a key */
int color_no; /* attribute we want to assign